/*
 * @Descripttion: 
 * @version: 
 * @Author: ChenXiang
 * @Date: 2021-08-19 22:41:29
 * @LastEditors: ChenXiang
 * @LastEditTime: 2021-08-20 09:08:46
 */
import React from "react";
export default function createForm(Cmp) {
    return class extends React.Component {
        constructor(props) {
            super(props);
            this.state = {}
            this.options = {}
        }
        getFieldDecorator = (filed, option) => InputCmp => {
            this.options[filed] = option;// 存放validateField
            return React.cloneElement(InputCmp, {
                value: this.state[filed],
                onChange: () => { }
            })
        }
        setFieldsValue = newStore => {
            this.setState(newStore)
        }
        getFieldsValue = () => {
            return this.state;
        }
        handleChange = (e) => {
            const { name, value } = e.target;
            this.setState({
                [name]: value
            })
        }
        validateField = (callback) => {
            // 校验规则 this.options
            // 校验值 this.state
            let arr = [];
            for (let field in this.options) {
                //判断state【filed】是否是undefined
                //是undefined err.push({filed:'err'})
            }
            if (err.length === 0) {
                //校验成功
                callback(null, this.state)
            } else {
                callback(err, this.state)
            }
        }
        getForm = () => {
            return {
                form: {
                    validateField: this.validateField,
                    getFieldDecorator: this.getFieldDecorator,
                    setFieldsValue: this.setFieldsValue,
                    getFieldsValue: this.getFieldsValue
                }
            }
        }

        render() {
            return <Cmp {...this.props} {...this.getForm()} />
        }
    }
}